package com.example.administrator.base.service;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.util.Log;

import com.allenliu.versionchecklib.core.AVersionService;
import com.example.administrator.base.bean.UpdataBean;
import com.example.administrator.base.util.GsonTools;
import com.example.administrator.base.util.SharedPreferencesUtils;



public class Updata extends AVersionService {
    public Updata() {
    }

    @Override
    public IBinder onBind(Intent intent) {

        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onResponses(AVersionService service, String response) {
        Log.i("21215121415",response);
        UpdataBean updatabean = GsonTools.changeGsonToBean(response, UpdataBean.class);
        if (updatabean != null) {
            if (updatabean.getCode() == 1) {
                int Vid = updatabean.getInfo().getVersionId();
                String mes = updatabean.getInfo().getRemarks();
                SharedPreferencesUtils.setParam(Updata.this, "uodatames", mes);
                if (Vid > getVersion()) {
                        service.showVersionDialog(updatabean.getInfo().getUrl(), "1", "1");
                    }
            }
        }
    }

    public int getVersion() {
        try {
            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            return info.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
            return 99;
        }
    }


}
